package com.jtlrm.ckd.mvp.view.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.base.sdk.base.net.CommonObserver;
import com.base.sdk.util.DateFormatUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.fragment.BaseFragment;
import com.jtlrm.ckd.entity.ShengHuaCheckEntity;
import com.jtlrm.ckd.entity.event.ShengHuaUpdateMessage;
import com.jtlrm.ckd.mvp.model.ShengHuaModel;
import com.jtlrm.ckd.mvp.view.activity.ShengHuaCheckDetailActivity;
import com.jtlrm.ckd.mvp.view.adapter.ShengHuaCheckAdapter;
import com.jtlrm.ckd.util.EventBusUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;

/**
 * 生化检查
 */
public class ShengHuaCheckFragment extends BaseFragment {
    private static final String ARG_PARAM1 = "patientId";
    private String patientId;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    ShengHuaCheckAdapter adapter;
    ShengHuaModel shengHuaModel;
    DateFormatUtil dateFormatUtil = new DateFormatUtil();

    public ShengHuaCheckFragment() {

    }


    public static ShengHuaCheckFragment newInstance(String patientId) {
        ShengHuaCheckFragment fragment = new ShengHuaCheckFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, patientId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            patientId = getArguments().getString(ARG_PARAM1);
        }
    }


    @Override
    protected int setContentLayout() {
        return R.layout.fragment_sheng_hua_check;
    }

    @Override
    protected void initView() {
        shengHuaModel = new ShengHuaModel();
        adapter = new ShengHuaCheckAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.addItemDecoration(createItemDecoration());
        recyclerView.setAdapter(adapter);
        View header = LayoutInflater.from(context).inflate(R.layout.shenghua_check_header, recyclerView, false);
        header.findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShengHuaCheckDetailActivity.goDetail(context, true, patientId);
            }
        });
        adapter.addHeaderView(header);
        EventBusUtil.register(this);
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {
        adapter.openLoadAnimation();
        smartRefreshLayout.setEnableLoadmore(false);
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                loadData();
            }
        });
        smartRefreshLayout.autoRefresh();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ShengHuaCheckEntity entity = (ShengHuaCheckEntity) adapter.getData().get(position);
                ShengHuaCheckDetailActivity.goDetail(context, false, patientId, entity.getId(), entity.getMeasureDate());
            }
        });
    }

    private void loadData() {
        shengHuaModel.queryRiQi(patientId, new CommonObserver<List<ShengHuaCheckEntity>>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
                closeFrshorLoadmore();
            }

            @Override
            public void onResult(List<ShengHuaCheckEntity> data) {
                String yuefen = "";
                try {
                    for (ShengHuaCheckEntity entity : data) {
                        String date = dateFormatUtil.getMonth(entity.getMeasureDate()) + "";
                        entity.setYueFen(date + "月");
                        if (date.equals(yuefen)) {
                            entity.setFirst(false);
                        } else {
                            entity.setFirst(true);
                        }
                        yuefen = date;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                adapter.replaceData(data);
                closeFrshorLoadmore();
            }
        }, lifecycleSubject);
    }


    @Override
    public void onDetach() {
        super.onDetach();

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ShengHuaUpdateMessage message) {
        if (message != null) {
            loadData();
        }
    }

    /**
     * 关闭加载更多或者刷新
     */
    private void closeFrshorLoadmore() {
        if (smartRefreshLayout.isLoading()) {
            smartRefreshLayout.finishLoadmore();
        }
        if (smartRefreshLayout.isRefreshing()) {
            smartRefreshLayout.finishRefresh();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregister(this);
    }


    protected RecyclerView.ItemDecoration createItemDecoration() {
        return new DefaultItemDecoration(ContextCompat.getColor(context, R.color.color_E5E5E5), 2, 1, -1);
    }

}
